package dongbai.soft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dongbai.soft.dao.ReceiptDao;
import dongbai.soft.entity.ReceiptEntity;
import dongbai.soft.service.ReceiptService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.Query;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("receiptService")
@SuppressWarnings("unchecked")
public class ReceiptServiceImpl extends ServiceImpl<ReceiptDao, ReceiptEntity> implements ReceiptService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Map<String, String> filter = (Map<String, String>) params.get("filter");
        IPage<ReceiptEntity> page = this.page(
                new Query<ReceiptEntity>().getPage(params),
                new QueryWrapper<ReceiptEntity>()
                        .between("receive_time", filter.get("start_date"), filter.get("end_date"))
                        .like("TRID", filter.get("TRID"))

        );

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> heatlist(int end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<BigDecimal> infos = redisTemplate.opsForList().range(Constant.PROFIT, 0, end);
        assert infos != null;
        List<Map<String, Object>> datas = new ArrayList<>();
        for (BigDecimal bigDecimal : infos) {
            Map<String, Object> map = new HashMap<>();
            map.put("date", sdf.format(DateUtils.addDays(new Date(), -end--)));
            map.put("value", bigDecimal);
            datas.add(map);
        }
        return datas;
    }


}